<template>
  <div class="register">
    <div class="registerBox">
      <h2>用户注册</h2>
      <el-form :model="userData" :rules="rules" ref="form">
        <el-form-item>
          <el-input
            v-model="userData.username"
            style="width: 300px"
            placeholder="用户名"
            clearable
        /></el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="userData.password"
            style="width: 300px"
            type="password"
            placeholder="请输入密码"
            show-password
          />
        </el-form-item>
        <el-form-item prop="confirmPassword">
          <el-input
            v-model="confirmPassword"
            style="width: 300px"
            type="password"
            placeholder="确认密码"
            show-password
          />
        </el-form-item>
      </el-form>
      <div class="registerBtn">
        <el-button
          type="primary"
          style="width: 100px; margin-bottom: 20px"
          @click="handleRegister"
          >注册</el-button
        >
        <el-button
          type="primary"
          style="width: 100px; margin-bottom: 20px"
          @click="router.back()"
          >返回登录</el-button
        >
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { registerHttp } from '@/request/index'
import { useUserDateStore } from '@/stores/user'
import { ElMessage } from 'element-plus'
import 'element-plus/theme-chalk/el-message.css'

const router = useRouter()
const { userData } = useUserDateStore()
const confirmPassword = ref('') // 确认密码

/* element-plus组件的alert消息提示 */
const open1 = () => {
  ElMessage({
    message: '注册成功',
    type: 'success',
    plain: true,
  })
}
const open2 = () => {
  ElMessage({
    message: '注册失败',
    type: 'warning',
    plain: true,
  })
}
const open3 = () => {
  ElMessage({
    message: '两次密码输入不一致',
    type: 'error',
    plain: true,
  })
}
// 密码规则
const validatePass = (rule, value, callback) => {
  if (userData.password === '') {
    callback(new Error('请输入密码'))
  }
}
const validatePass2 = (rule, value, callback) => {
  if (confirmPassword.value === '') {
    callback(new Error('请再次输入密码'))
  } else if (confirmPassword.value !== userData.password) {
    callback(new Error('两次密码输入不一致'))
  } else {
    callback()
  }
}

const rules = {
  password: [{ validator: validatePass, trigger: 'blur' }],
  confirmPassword: [{ validator: validatePass2, trigger: 'blur' }],
}
// 注册用户
const handleRegister = async () => {
  const { password } = userData
  if (password !== confirmPassword.value) {
    open3()
    return
  }
  const res = await registerHttp(userData)
  if (res && res.code === 200) {
    open1()
  } else {
    open2()
  }
  // 发送请求
}
</script>

<style lang="scss" scoped>
.register {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-image: url('@/assets/images/bg.jpg');
  .registerBox {
    width: 450px;
    padding: 20px;
    margin: auto;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    gap: 25px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.4);
    border-radius: 10px;
    background-color: #fff;
    h2 {
      text-align: center;
      font-size: 30px;
      color: #333;
      margin-bottom: 20px;
    }
    .registerBtn {
      display: flex;
      justify-content: center;
      align-items: center;
      gap: 20px;
      margin-top: 20px;
    }
  }
}
</style>
